/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lfk.demo.Ability;

import com.lfk.EventHelper;
import com.lfk.demo.ResourceTable;
import com.lfk.justwetools.Component.LineProgress.LineProgress;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.aafwk.ability.delegation.IAbilityDelegator;
import ohos.agp.components.Button;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ViewDemoAbilityTest {

    private static IAbilityDelegator sAbilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
    private ViewDemoAbility ability;
    private Button button;
    private LineProgress lineProgress;

    @Before
    public void setUp() throws Exception {
        ability = EventHelper.startAbility(ViewDemoAbility.class);
        EventHelper.waitForActive(ability, 5);
        Thread.sleep(2000);

        button = (Button) ability.findComponentById(ResourceTable.Id_button);
        lineProgress = (LineProgress) ability.findComponentById(ResourceTable.Id_linepro);
    }

    @After
    public void tearDown() throws Exception {
        Thread.sleep(500);
        sAbilityDelegator.stopAbility(ability);
        EventHelper.clearAbilities();
    }

    @Test
    public void testLineProgress() throws Exception {
        float srcProgress = lineProgress.getProgressing();
        sAbilityDelegator.runOnUIThreadSync(() -> button.simulateClick());
        Thread.sleep(200);

        float dstProgress = lineProgress.getProgressing();
        assertTrue("srcProgress:" + srcProgress + ", dstProgress:" + dstProgress, srcProgress < dstProgress);

        for (int i = 0; i < 9; i++) {
            sAbilityDelegator.runOnUIThreadSync(() -> button.simulateClick());
            Thread.sleep(200);
        }
        assertEquals(100f, lineProgress.getProgressing(), 0.001f);

    }
}